/**
 * BaseController.java
 * Created at 2016年5月12日
 * Created by Jackie Liu
 * Copyright (C) 2016, All rights reserved.
 */
package com.yunfeisoft.common.base;

import com.yunfeisoft.common.editor.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * <p>ClassName: BaseController</p>
 * <p>Description: controller基类</p>
 * <p>Author: Jackie Liu</p>
 * <p>Date: 2016年5月12日</p>
 */
public class BaseController {

    protected static final Logger log = LoggerFactory.getLogger(BaseController.class);

    @InitBinder
    protected void initBinder(ServletRequestDataBinder binder) {
		/*SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));*/
		
		/*binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, null, true));
		binder.registerCustomEditor(Long.class, null, new CustomNumberEditor(Long.class, null, true));
		binder.registerCustomEditor(Double.class, null, new CustomNumberEditor(Double.class, null, true));
		binder.registerCustomEditor(int.class, null, new CustomNumberEditor(Integer.class, null, true));
		binder.registerCustomEditor(long.class, null, new CustomNumberEditor(Long.class, null, true));
		binder.registerCustomEditor(double.class, null, new CustomNumberEditor(Double.class, null, true));*/

        binder.registerCustomEditor(Date.class, null, new DateEditor());
        binder.registerCustomEditor(Integer.class, null, new IntegerObjectEditor());
        binder.registerCustomEditor(Long.class, null, new LongObjectEditor());
        binder.registerCustomEditor(Double.class, null, new DoubleObjectEditor());
        binder.registerCustomEditor(Short.class, null, new ShortObjectEditor());
        binder.registerCustomEditor(int.class, null, new IntegerEditor());
        binder.registerCustomEditor(long.class, null, new LongEditor());
        binder.registerCustomEditor(double.class, null, new DoubleEditor());
        binder.registerCustomEditor(short.class, null, new ShortEditor());
    }

    protected void initParams(Map<String, Object> params, HttpServletRequest request) {
        int pageSize = ServletRequestUtils.getIntParameter(request, "rows", 20);
        int pageNum = ServletRequestUtils.getIntParameter(request, "page", 1);
        params.put("pageSize", pageSize);
        params.put("pageNum", pageNum);
    }
}
